package com.wpw.dream.spider.processor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wpw.dream.spider.entity.Content;
import com.wpw.dream.spider.entity.SpiderConfig;
import com.wpw.dream.spider.enums.MatcherTypeEnum;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

public class SpiderPageProcessor implements PageProcessor {
	
	private SpiderConfig spiderConfig;
	
	public SpiderPageProcessor(SpiderConfig spiderConfig) {
		this.spiderConfig = spiderConfig;
	}
	
	@Override
	public void process(Page page) {
		if (page.getUrl().regex(spiderConfig.getListMatcher()).match()) {
			//获取链接
			Html html = page.getHtml();
			//List<String> parseArticalUrl = parseArticalUrl(html, spiderConfig);
			
            page.addTargetRequests(parseArticalUrl(html, spiderConfig));
            page.addTargetRequests(parseNewPageUrl(html, spiderConfig));
            
        } else {
        	Content content = makeContent(page, spiderConfig);
        	if (content == null) {
        		page.setSkip(true);
        	} else {
        		content.setCatalogId(spiderConfig.getCatalogId());
        		content.setSpiderTime(System.currentTimeMillis());
        		content.setLinkUrl(page.getUrl().toString());
        		content.setSpiderId(spiderConfig.getId());
        		if (spiderConfig.getMaxArticleTime() != null && content.getArticleTime() <= spiderConfig.getMaxArticleTime()) {
        			page.setSkip(true);
        			Map<String, Object> extras = new HashMap<>();
        			extras.put("stop", true);
        			page.getRequest().setExtras(extras);
        		} else {
        			page.putField("content", content);
        		}
        	}
        }
	}

	private Content makeContent(Page page, SpiderConfig spiderConfig) {
		Content content = new Content();
		if (MatcherTypeEnum.XPATH.getValue().equals(spiderConfig.getTitleMatcherType())) {
			content.setTitle(page.getHtml().xpath(spiderConfig.getTitleMatcher()).toString());
		} else if (MatcherTypeEnum.SELECTOR.getValue().equals(spiderConfig.getTitleMatcherType())) {
			content.setTitle(page.getHtml().$(spiderConfig.getTitleMatcher()).toString());
		} else if (MatcherTypeEnum.REG.getValue().equals(spiderConfig.getTitleMatcherType())) {
			content.setTitle(page.getHtml().regex(spiderConfig.getTitleMatcher()).toString());
		} else {
			return null;
		}
		
//		if (MatcherTypeEnum.XPATH.getValue().equals(spiderConfig.getShorttitleMatcherType())) {
//			content.setShortTitle(page.getHtml().xpath(spiderConfig.getShorttitleMatcher()).toString());
//		} else if (MatcherTypeEnum.SELECTOR.getValue().equals(spiderConfig.getShorttitleMatcherType())) {
//			content.setShortTitle(page.getHtml().$(spiderConfig.getShorttitleMatcher()).toString());
//		} else if (MatcherTypeEnum.REG.getValue().equals(spiderConfig.getShorttitleMatcherType())) {
//			content.setShortTitle(page.getHtml().regex(spiderConfig.getShorttitleMatcher()).toString());
//		} else {
//			return null;
//		}
		
		if (MatcherTypeEnum.XPATH.getValue().equals(spiderConfig.getContentMatcherType())) {
			content.setContent(page.getHtml().xpath(spiderConfig.getContentMatcher()).toString());
		} else if (MatcherTypeEnum.SELECTOR.getValue().equals(spiderConfig.getContentMatcherType())) {
			content.setContent(page.getHtml().$(spiderConfig.getContentMatcher()).toString());
		} else if (MatcherTypeEnum.REG.getValue().equals(spiderConfig.getContentMatcherType())) {
			content.setContent(page.getHtml().regex(spiderConfig.getContentMatcher()).toString());
		} else {
			return null;
		}
		
		if (MatcherTypeEnum.XPATH.getValue().equals(spiderConfig.getArticletimeMatchertype())) {
			content.setArticleTime(getTime(page.getHtml().xpath(spiderConfig.getArticletimeMatcher()).toString(), spiderConfig.getArticletimeFormat()));
		} else if (MatcherTypeEnum.SELECTOR.getValue().equals(spiderConfig.getArticletimeMatchertype())) {
			content.setArticleTime(getTime(page.getHtml().$(spiderConfig.getArticletimeMatcher()).toString(), spiderConfig.getArticletimeFormat()));
		} else if (MatcherTypeEnum.REG.getValue().equals(spiderConfig.getArticletimeMatchertype())) {
			content.setArticleTime(getTime(page.getHtml().regex(spiderConfig.getArticletimeMatcher()).toString(), spiderConfig.getArticletimeFormat()));
		} else {
			return null;
		}
		
		
		return content;
	}
	
	private Long getTime(String timeStr, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			return formatter.parse(timeStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0L;
	
		
	}

	/**
	 * 解析页码url
	 * @Description：
	 * @author: wupengwei
	 * @date: 2017年3月7日 下午3:21:15
	 * @param html
	 * @param spiderConfig
	 * @return
	 */
	private List<String> parseNewPageUrl(Html html, SpiderConfig spiderConfig) {
		return html.links().regex(spiderConfig.getListMatcher()).all();
	}

	/**
	 * 解析文章url
	 * @Description：
	 * @author: wupengwei
	 * @date: 2017年3月7日 下午3:20:56
	 * @param html
	 * @param spiderConfig
	 * @return
	 */
	private List<String> parseArticalUrl(Html html, SpiderConfig spiderConfig) {
		if (MatcherTypeEnum.XPATH.getValue().equals(spiderConfig.getArticleUrlMatcherType())) {
			return html.xpath(spiderConfig.getArticleUrlMatcher()).links().all();
		} else if (MatcherTypeEnum.SELECTOR.getValue().equals(spiderConfig.getArticleUrlMatcherType())) {
			return html.$(spiderConfig.getArticleUrlMatcher()).links().all();
		} else if (MatcherTypeEnum.REG.getValue().equals(spiderConfig.getArticleUrlMatcherType())) {
			return html.links().regex(spiderConfig.getArticleUrlMatcher()).links().all();
		} else {
			return html.links().regex(spiderConfig.getArticleUrlMatcher()).all();
		}
	}

	@Override
	public Site getSite() {
		return Site.me()
				.setDomain(spiderConfig.getSourceUrl().split("/")[2])
				.setCharset(spiderConfig.getCharset())
				.setTimeOut(spiderConfig.getTimeOut())
				.setSleepTime(spiderConfig.getSleepTime())
				.setCycleRetryTimes(5)
				.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
	}

}
